package com.tr.rentcar.controller.houtai.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.model.bean.CarStyle;
import com.tr.rentcar.model.bean.Employee;
import com.tr.rentcar.model.bean.Order;
import com.tr.rentcar.model.bean.OrderRecord;
import com.tr.rentcar.model.dao.EmployeeDao;
import com.tr.rentcar.model.dao.OrderDao;
import com.tr.rentcar.model.dao.OrderRecordDao;

@WebServlet("/houtaiorder")
public class OrderControllor extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrderDao od = new OrderDao();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			String tel = req.getParameter("tel");
			int pageSize=10;//每页显示记录数
			int pageNo=1;//默认当前页为1
			if(req.getParameter("currentPage")!=null){
				pageNo=Integer.parseInt(req.getParameter("currentPage"));
			}
			req.setAttribute("pageno", pageNo);
			//获取所有空闲员工
			List<Employee> elist = new EmployeeDao().getAllByFree();
			req.setAttribute("elist", elist);
			//获取订单数量
			Long count=od.getAllCount();
			req.setAttribute("count", count);
			List<Order> order =null;
			if(tel==null){
				order=od.findAllExcNopay(pageNo,pageSize);
			}else{
				order=od.findByNoAndTel(tel);
			}
			req.setAttribute("Order", order);
		} catch (SQLException e) {
			req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
			e.printStackTrace();
		}

		req.getRequestDispatcher("/WEB-INF/jsp/houtai_order.jsp").forward(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			int oid=Integer.parseInt(req.getParameter("oid"));
			//获取订单操作记录
			List<OrderRecord> orlist=new OrderRecordDao().getAll(oid);
			resp.getWriter().println(JSONArray.toJSONString(orlist));
		} catch (SQLException e) {
			req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
			e.printStackTrace();
		}
	}

}
